package com.tinyseed.emcp.api.productionRule.rest;

import com.alibaba.fastjson.JSONObject;
import com.tinyseed.emcp.api.dashboard.query.TreeQuickQuery;
import com.tinyseed.emcp.api.dashboard.service.DashboardService;
import com.tinyseed.emcp.api.productionRule.bean.DistributionInputDTO;
import com.tinyseed.emcp.api.productionRule.bean.DistributionOutputDTO;
import com.tinyseed.emcp.api.productionRule.service.DistributionService;
import com.tinyseed.emcp.api.productionRule.service.ReductionService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/distribution")
@Api("重污染天气应急接口")
public class DistributionController {

    @Autowired
    private DistributionService distributionService;
    @Autowired
    private ReductionService reductionService;

    @Autowired
    private DashboardService dashboardService;

    /**
     * 获取设备分组界面树结构
     */
    @PostMapping(value = "/queryTree")
    @ResponseBody
    public ResponseEntity queryTree() {
        List<TreeQuickQuery> treeQuickQueries = dashboardService.allMonitorRelationTree();
        return new ResponseEntity<>(treeQuickQueries,HttpStatus.OK);

    }
    /**
     * 获取停限产列表
     */
    @PostMapping(value = "/queryReductionList")
    @ResponseBody
    public ResponseEntity queryReductionList() {
        List<Map<String, String>> list = distributionService.queryReductionList();
        return new ResponseEntity<>(list,HttpStatus.OK);

    }

    /**
     * 选择方案
     */
    @PostMapping(value = "/selectByProducesetId")
    @ResponseBody
    public ResponseEntity selectByProducesetId(DistributionInputDTO distributionInputDTO) {
        List<DistributionOutputDTO> list = distributionService.findAllByProducesetId(distributionInputDTO.getProducesetId());
        return new ResponseEntity<>(list,HttpStatus.OK);

    }

    /**
     * 通过monitorId查找方案
     */
    @PostMapping(value = "/selectByMonitorId")
    @ResponseBody
    public ResponseEntity selectByMonitorId(DistributionInputDTO distributionInputDTO) {
//        List<DistributionOutputDTO> list = distributionService.findAllByMonitorId(distributionInputDTO.getMonitorId());
        return new ResponseEntity<>(HttpStatus.OK);

    }
    /**
     * 保存方案
     */
    @PostMapping(value = "/saveDistributions")
    @ResponseBody
    public ResponseEntity saveDistributions(@RequestBody DistributionInputDTO distributionInputDTO) {
        distributionService.saveDistributions(distributionInputDTO);
        return new ResponseEntity<>(HttpStatus.OK);

    }

    @PostMapping(value = "/saveRules")
    @ResponseBody
    public ResponseEntity saveRules(@RequestBody List<DistributionInputDTO> distributionInputDTO) {

        distributionService.saveRules(distributionInputDTO);
        return new ResponseEntity<>(HttpStatus.OK);

    }

    /**
     * 删除方案关系
     */
    @PostMapping(value = "/deleteDistributions")
    @ResponseBody
    public ResponseEntity deleteAllByProducesetIdIsIn(@RequestBody String producesetIds) {
        List<Long> producestIdes = JSONObject.parseArray(producesetIds, Long.class);
        distributionService.deleteAllByProducesetIdIsIn(producestIdes);
        return new ResponseEntity<>(HttpStatus.OK);

    }

}
